<?php
namespace app\common\controller;

use think\Db;
use app\common\adapter\AuthAdapter;
use app\common\controller\Common;


/**
 * this interface look like the ApiCommon, they are both to check user login info,
 * but the different of them is, the interface ignore the user permission setting,
 * and allow user to access the derive class's action.
 *
 */
class ApiPublic extends Common
{
	public function initialize()
	{
		parent::initialize();
		$authKey = $this->request->header('authkey');
		$cache = cache('Auth_'.$authKey);
		
		// 校验sessionid和authKey
		if (strlen($authKey)<=32) {
			header('Content-Type:application/json; charset=utf-8');
			exit(json_encode(['code'=>STATUS_RE_AUTH, 'errmsg'=>'需要登陆'.$authKey.'+'.strlen($authKey)]));
		}
		
		if (empty($cache)){
			$userModel=new \app\admin\model\AdminUser;
			if ($userModel->createUserCache($authKey)){
				$cache = cache('Auth_'.$authKey);
			} else {
				header('Content-Type:application/json; charset=utf-8');
				exit(json_encode(['code'=>STATUS_RE_AUTH, 'errmsg'=>'登录失效或账号已被禁用']));
			}
		}
		

		$GLOBALS['userInfo'] = $cache['UserInfo'];
	}
	

}
